<?php

class PinjamanController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/template';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','create','simulasi','Delete_Pinjaman','laporanPinjaman','otomasiJurnal','insertPokok'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$criteria = new CDbCriteria;
			
			if (isset($_REQUEST['sSearch']) && isset($_REQUEST['sSearch']{0})) {
				  $criteria->addSearchCondition('anggota.NAMA', $_REQUEST['sSearch'], true, 'OR', 'LIKE');
				  $criteria->addSearchCondition('TANGGAL_TRANSAKSI', $_REQUEST['sSearch'], true, 'OR', 'LIKE');
				  $criteria->addSearchCondition('JUMLAH_PINJAMAN', $_REQUEST['sSearch'], true, 'OR', 'LIKE');
				  $criteria->addSearchCondition('SALDO', $_REQUEST['sSearch'], true, 'OR', 'LIKE');
				  $criteria->addSearchCondition('TERMINE', $_REQUEST['sSearch'], true, 'OR', 'LIKE');
				  $criteria->addSearchCondition('CICILAN_TERBAYAR', $_REQUEST['sSearch'], true, 'OR', 'LIKE');
			 }

			$sort = new EDTSort('Pinjaman', array('anggota.NAMA','TANGGAL_TRANSAKSI','JUMLAH_PINJAMAN','SALDO','TERMINE','CICILAN_TERBAYAR'));
			 $sort->defaultOrder = 'ID_PINJAMAN';
			 $pagination = new EDTPagination();
			 
			 $dataProvider = new CActiveDataProvider('Pinjaman', array(
				  'criteria'      => $criteria,
				  'pagination'    => $pagination,
				  'sort'          => $sort,
			 ));
			 
			$widget=$this->createWidget('ext.EDataTables.EDataTables', array(
			 'id'            => 'Pinjaman',
			 'dataProvider'  => $dataProvider,
			 'ajaxUrl'       => Yii::app()->getBaseUrl().'/index.php/Anggota',
			 'columns'       => array('anggota.NAMA','TANGGAL_TRANSAKSI','JUMLAH_PINJAMAN','SALDO','TERMINE','CICILAN_TERBAYAR'
								),
			'buttons' => array(
					// 'refresh' => null,
					'print' => null,
					'export' => null,
					'klaim' => array(
								 'label' => 'Tambah Pinjaman',
								 'text' => true,
								 'htmlClass' => 'refreshButton',
								 'icon' => 'ui-icon-document',
								  'callback' => 'js:function(){document.location.href = \''.Yii::app()->request->baseUrl.'/index.php/Pinjaman/create\';}',
								),

				)
			 
			));		
		
			if (!Yii::app()->getRequest()->getIsAjaxRequest()) {
				$this->render('index', array('widget' => $widget));
				return;
			} else {
			  echo json_encode($widget->getFormattedData(intval($_REQUEST['sEcho'])));
			  Yii::app()->end();
			}
	}
	
	
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$criteria = new CDbCriteria;
		$criteria->with=array('asal');
		$criteria->condition="ID_ANGGOTA=".$id." AND STATUS_LUNAS=0";
			
			if (isset($_REQUEST['sSearch']) && isset($_REQUEST['sSearch']{0})) {
				  
			 }

			$sort = new EDTSort('Pinjaman', array('anggota.NO_ANGGOTA','anggota.NAMA','JUMLAH_PINJAMAN','TERMINE','CICILAN_TERBAYAR','SALDO'));
			 $sort->defaultOrder = 'ID_PINJAMAN';
			 $pagination = new EDTPagination();
			 
			 $dataProvider = new CActiveDataProvider('Pinjaman', array(
				  'criteria'      => $criteria,
				  'pagination'    => $pagination,
				  'sort'          => $sort,
			 ));
			 
			$widget=$this->createWidget('ext.EDataTables.EDataTables', array(
			 'id'            => 'Pinjaman',
			 'dataProvider'  => $dataProvider,
			 'ajaxUrl'       => Yii::app()->getBaseUrl().'/index.php/Pinjaman/view',
			 'columns'       => array('anggota.NO_ANGGOTA','anggota.NAMA',
									array(
									'name'=>'JUMLAH_PINJAMAN',
									'value'=>'"Rp ".number_format($data->JUMLAH_PINJAMAN,2,",",".")',
									),
									array(
									'name'=>'TERMINE',
									'value'=>'$data->TERMINE." bulan"',
									),
									array(
									'name'=>'isActive',
									'header'=>'Cicilan per Bulan',
									//'filter'=>array('1'=>'Sudah','0'=>'Belum'),
									// 'value'=>'"Rp ".number_format((round((($data->JUMLAH_PINJAMAN/$data->TERMINE)+500/2)/500)*500),2,",",".")'
									'value'=>'(($data->JUMLAH_PINJAMAN/$data->TERMINE)%500==0 )?("Rp ".number_format(($data->JUMLAH_PINJAMAN/$data->TERMINE),2,",",".")):("Rp ".number_format((round((($data->JUMLAH_PINJAMAN/$data->TERMINE)+500/2)/500)*500),2,",","."))'
									),
									'CICILAN_TERBAYAR',
									array(
									'name'=>'SALDO',
									'value'=>'"Rp ".number_format($data->SALDO,2,",",".")',
									),
									'asal.ASAL_PINJAMAN',
								array('class'=>'ext.EDataTables.EButtonColumn',
												'header'=>'Historis Pembayaran',
												'template'=>'{myButton}',
												'buttons'=>array(
														'myButton'=>array(
												'label'=>'Lihat Historis Pembayaran Pinjaman',
												// 'visible'=>'FALSE',
												'url'=>'Yii::app()->createUrl("PembayaranPinjaman/index/".$data->ID_PINJAMAN)',
												'imageUrl'=>Yii::app()->request->baseUrl.'/images/grid/pencil-icon.png',
																		),
																),
												),
								array('class'=>'ext.EDataTables.EButtonColumn',
												'header'=>'Edit Pokok Pinjaman',
												'template'=>'{myButton}',
												'buttons'=>array(
														'myButton'=>array(
												'label'=>'Masukkan Pokok Pinjaman',
												// 'visible'=>'FALSE',
												'url'=>'Yii::app()->createUrl("Pinjaman/insertPokok/".$data->ID_PINJAMAN)',
												'imageUrl'=>Yii::app()->request->baseUrl.'/images/grid/user_edit.png',
																		),
																),
												),
								array(
        'class'=>'ext.EDataTables.EButtonColumn',
		'template'=>'{delete}',
		'header'=>'Hapus Pinjaman',
		'buttons'=>array(
					'delete'=>array(
			'label'=>'Hapus Pinjaman',
'url'=>'Yii::app()->createUrl("Pinjaman/Delete_Pinjaman/".$data->ID_PINJAMAN."/"."'.$id.'")',
			'imageUrl'=>Yii::app()->request->baseUrl.'/images/grid/deleted.png',
									),
							),
        'deleteConfirmation'=>"js:'Hapus Pinjaman Anggota  '+$(this).parent().parent().children(':nth-child(2)').text()+' dengan Jumlah '+$(this).parent().parent().children(':nth-child(3)').text()+' dari daftar?'",		),
								
								),
			'buttons' => array(
					// 'refresh' => null,
					'print' => null,
					'export' => null,
					'klaim' => array(
								 'label' => 'Input Pinjaman',
								 'text' => true,
								 'htmlClass' => 'refreshButton',
								 'icon' => 'ui-icon-document',
'callback' => 'js:function(){document.location.href = \''.Yii::app()->request->baseUrl.'/index.php/Pinjaman/create/'.$id.'\';}',
								),
					'back' => array(
								 'label' => 'Kembali',
								 'text' => true,
								 'htmlClass' => 'refreshButton',
								 'icon' => 'ui-icon-document',
'callback' => 'js:function(){document.location.href = \''.Yii::app()->request->baseUrl.'/index.php/SimpanPinjam\';}',
								),

				)
			 
			));		
		
			if (!Yii::app()->getRequest()->getIsAjaxRequest()) {
				$this->render('view', array('widget' => $widget));
				return;
			} else {
			  echo json_encode($widget->getFormattedData(intval($_REQUEST['sEcho'])));
			  Yii::app()->end();
			}
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate($id)
	{
		$model=new Pinjaman;
		$model->ID_ANGGOTA = $id;
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Pinjaman']))
		{
			$model->attributes=$_POST['Pinjaman'];
			$model->TANGGAL_TRANSAKSI = date('Y-m-d', strtotime($model->TANGGAL_TRANSAKSI));
			$model->TERMINE = intval($_POST['Pinjaman']['TERMINE']);
			
			$pinjaman = intval($_POST['Pinjaman']['JUMLAH_PINJAMAN']) + (intval($_POST['Pinjaman']['JUMLAH_PINJAMAN']) * intval($model->TERMINE) * 0.01);
			// $round_up = (ceil(($pinjaman/$model->TERMINE)/1000)*1000) * $model->TERMINE;
			
			$cicilan = $pinjaman/$model->TERMINE;
			if ($cicilan % 500 == 0){
				$round_up = $cicilan;
			}
			else {
				$round_up = round(($cicilan+500/2)/500)*500;
			}
			
			$model->JUMLAH_PINJAMAN = $round_up*$model->TERMINE;
			$model->SALDO = $model->JUMLAH_PINJAMAN;
			
			$model->ASAL_PINJAMAN = $_POST['Pinjaman']['ASAL_PINJAMAN'];
			$model->POKOK_PINJAMAN = $_POST['Pinjaman']['JUMLAH_PINJAMAN'];
			
			$anggota = Anggota::model()->find("ID_ANGGOTA=".$id);
			
			if($model->save()){
				$this->otomasiJurnal($model->TANGGAL_TRANSAKSI,$anggota->NAMA,$model->JUMLAH_PINJAMAN,$model->ID_PINJAMAN);
				$this->redirect(array('view','id'=>$model->ID_ANGGOTA));
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Pinjaman']))
		{
			$model->attributes=$_POST['Pinjaman'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->ID_PINJAMAN));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	
	public function actionDelete_Pinjaman($id,$idx)
	{
		$pinjaman = Pinjaman::model()->find("ID_PINJAMAN=".$id);
		$history = PembayaranPinjaman::model()->deleteAll("ID_PINJAMAN=".$id);
		if($pinjaman->delete()){
			$this->redirect(array('view','id'=>$idx));
		}
	}
	

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Pinjaman('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Pinjaman']))
			$model->attributes=$_GET['Pinjaman'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}
	
	public function actionSimulasi($id,$idx)
	{
		$this->layout='//layouts/template_simulasi';
		$termin = $idx;
		$total = $id + ($id * $termin * 0.01);
		$this->render('simulasi',array('total'=>$total,'jumlah'=>$id,'termin'=>$termin));
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Pinjaman::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='pinjaman-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	public function actionlaporanPinjaman()
	{
		if(isset($_POST['tanggal_awal']) && isset($_POST['tanggal_akhir'])){
			$tanggal_awal = date('Y-m-d', strtotime($_POST['tanggal_awal']));
			$tanggal_akhir = date('Y-m-d', strtotime($_POST['tanggal_akhir']));
			
			if ($_POST['Anggota']['STATUS_PEGAWAI'] != "")
				$status_anggota= " AND anggota.STATUS_PEGAWAI=".$_POST['Anggota']['STATUS_PEGAWAI'];

			else 
				$status_anggota ="";
				
			if ($_POST['AsalPinjaman']['ASAL_PINJAMAN'] != ""){
				$status_pinjaman = " AND ASAL_PINJAMAN=".$_POST['AsalPinjaman']['ASAL_PINJAMAN'];
				$asalpinjaman=AsalPinjaman::model()->find("ID_ASAL_PINJAMAN=".$_POST['AsalPinjaman']['ASAL_PINJAMAN']);
				$string_pinjaman= '(PINJAMAN DARI '.strtoupper($asalpinjaman->ASAL_PINJAMAN).')';
			}
			else{
				$status_pinjaman="";
				$string_pinjaman="";
			}
			
			$data = Pinjaman::model()->with('anggota')->findAll("TANGGAL_TRANSAKSI BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'".$status_anggota.$status_pinjaman);
			
			if($_POST['Anggota']['STATUS_PEGAWAI']==2){
				$status_pegawai = "( PEGAWAI : BLUD )";
			}
			else if($_POST['Anggota']['STATUS_PEGAWAI']==1){
				$status_pegawai = "( PEGAWAI : TETAP )";
			} 
			else 
				$status_pegawai = "( PEGAWAI : SEMUA PEGAWAI )";
			
			
			$objPHPExcel = new PHPExcel();
			$sheet = $objPHPExcel->setActiveSheetIndex(0);  
			
			 $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
			 $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(35);
			 $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
			 $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
			 $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
			 $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
			 $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(25);
			 $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(25);
			
			
			$sheet->mergeCells('A1:F1')->setCellValue('A1', 'DAFTAR PINJAMAN ANGGOTA')->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('A2:F2')->setCellValue('A2', 'KOPERASI RUMAH SAKIT HAJI SURABAYA')->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('A3:F3')->setCellValue('A3', $string_pinjaman.$status_pegawai)->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('A4:F4')->setCellValue('A4', 'PERIODE : '.$_POST['tanggal_awal'].' s/d '.$_POST['tanggal_akhir'] )->getStyle('A4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			$sheet->getStyle("A1:A2")->applyFromArray(array("font" => array( "bold" => true, "size"=>18)));
			$sheet->getStyle("A3:A4")->applyFromArray(array("font" => array( "bold" => true, "size"=>12)));
			$sheet->getStyle("A6:H6")->applyFromArray(array("font" => array( "bold" => true, "size"=>10)));
			$sheet->getStyle("D7:E7")->applyFromArray(array("font" => array( "bold" => true, "size"=>10)));
			
			// Add some data
			$sheet->mergeCells('A6:A7')->setCellValue('A6', 'NO.')->getStyle('A6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('B6:B7')->setCellValue('B6', 'NAMA ANGGOTA')->getStyle('B6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('C6:C7')->setCellValue('C6', "JUMLAH PINJAMAN \r (Rp)")->getStyle('C6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('D6:D7')->setCellValue('D6', "TERMINE")->getStyle('D6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('E6:E7')->setCellValue('E6', "CICILAN / BULAN \r (Rp)")->getStyle('E6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('F6:F7')->setCellValue('F6', "CICILAN KE")->getStyle('F6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('G6:G7')->setCellValue('G6', "SALDO \r (Rp)")->getStyle('G6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$sheet->mergeCells('H6:H7')->setCellValue('H6', "ASAL PINJAMAN")->getStyle('H6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			$sheet->getStyle('A6')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$sheet->getStyle('B6')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$sheet->getStyle('C6')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$sheet->getStyle('D6')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$sheet->getStyle('E6')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$sheet->getStyle('F6')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$sheet->getStyle('G6')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$sheet->getStyle('H6')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			
			$sheet->getStyle('A6')->getAlignment()->setWrapText(true);
			$sheet->getStyle('B6')->getAlignment()->setWrapText(true);
			$sheet->getStyle('C6')->getAlignment()->setWrapText(true);
			$sheet->getStyle('D6')->getAlignment()->setWrapText(true);
			$sheet->getStyle('E6')->getAlignment()->setWrapText(true);
			$sheet->getStyle('F6')->getAlignment()->setWrapText(true);
			$sheet->getStyle('G6')->getAlignment()->setWrapText(true);
			$sheet->getStyle('H6')->getAlignment()->setWrapText(true);
			
			$sheet->getStyle("A6:H6")->applyFromArray(array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THICK))));
			$sheet->getStyle("A7:H7")->applyFromArray(array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THICK))));
			
			$no = 1;$i = 8;
			
			foreach($data as $row){
				$cicilan = $row->JUMLAH_PINJAMAN / $row->TERMINE;
				$sheet->setCellValue('A'.$i, $no);
				$sheet->setCellValue('B'.$i, $row->anggota['NAMA']);
				$sheet->setCellValue('C'.$i, $row->JUMLAH_PINJAMAN)->getStyle('C'.$i)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
				$sheet->setCellValue('D'.$i, $row->TERMINE);
				$sheet->setCellValue('E'.$i, $cicilan)->getStyle('E'.$i)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
				$sheet->setCellValue('F'.$i, $row->CICILAN_TERBAYAR);
				$sheet->setCellValue('G'.$i, $row->SALDO)->getStyle('G'.$i)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
				$sheet->setCellValue('H'.$i, $row->asal['ASAL_PINJAMAN']);
				
				$sheet->getStyle("A".$i.":H".$i)->applyFromArray(array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN))));
				
				$no++;$i++;
			}
			
			// Save a xls file
			$filename = 'Laporan Pinjaman Anggota Periode '.$_POST['tanggal_awal'].' s/d '.$_POST['tanggal_akhir'];
			header('Content-Type: application/vnd.ms-excel');
			header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
			header('Cache-Control: max-age=0');
			
			$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

			$objWriter->save('php://output');
			unset($this->objWriter);
			unset($this->objWorksheet);
			unset($this->objReader);
			unset($this->objPHPExcel);
			exit();
			
		}
		else 
			$this->render('form_laporan_pinjaman');
	}
	
	public function otomasiJurnal($tanggal,$nama,$jumlah,$id_pinjaman){
		$otomasi = AkunOtomasi::model()->findByPk(3);
				
				$jurnal = new Jurnal;
				$last_jurnal = Jurnal::model()->find(array("order" => "ID_JURNAL DESC"));
				
				if ($last_jurnal == null){
					$no_jurnal = 1;
				}
				else {
					$split = explode("-",$last_jurnal->NO_JURNAL);
					$no_jurnal = $split[1] + 1;
				}
				
				$jurnal->NO_JURNAL = "JU-".$no_jurnal;
				$jurnal->TANGGAL = $tanggal;
				$jurnal->KETERANGAN = "Pinjaman ".$nama;
				
				$jurnal->save();
				
				$help = new HelpPinjamanJurnal;
				$help->ID_PINJAMAN = $id_pinjaman;
				$help->ID_JURNAL = $jurnal->ID_JURNAL;
				
				$help->save();
				
				$jurnal_detail_debet = new JurnalDetail;
				$jurnal_detail_debet->ID_JURNAL = $jurnal->ID_JURNAL;
				$jurnal_detail_debet->ID_AKUN = $otomasi->OTOMASI_DEBET;
				$jurnal_detail_debet->DEBIT_KREDIT = 1;
				$jurnal_detail_debet->NILAI = $jumlah;
				
				$jurnal_detail_debet->save();
				
				$jurnal_detail_kredit = new JurnalDetail;
				$jurnal_detail_kredit->ID_JURNAL = $jurnal->ID_JURNAL;
				$jurnal_detail_kredit->ID_AKUN = $otomasi->OTOMASI_KREDIT;
				$jurnal_detail_kredit->DEBIT_KREDIT = 0;
				$jurnal_detail_kredit->NILAI = $jumlah;
				
				$jurnal_detail_kredit->save();
	}
	
	public function actioninsertPokok($id){
		$model= Pinjaman::model()->find("ID_PINJAMAN=".$id);
		
		if(isset($_POST['Pinjaman'])){
			$model->POKOK_PINJAMAN = $_POST['Pinjaman']['POKOK_PINJAMAN'];
			if($model->save()){
				$this->redirect(array('view','id'=>$model->ID_ANGGOTA));
			}
		}
		$this->render('insertPokok',array(
			'model'=>$model,
		));
	}
	
}
